package com.example.redis.demos.web.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Set;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-07-13
 * Time:12:04
 * Text：
 */
@RestController
public class Redis {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/testString")
    @ResponseBody
    public void testString(){
        redisTemplate.opsForValue().set("key","111");
        redisTemplate.opsForValue().set("key2","222");
        redisTemplate.opsForValue().set("key3","333");
        redisTemplate.opsForValue().set("key4","444");

        String value = redisTemplate.opsForValue().get("key");
        System.out.println(value);

    }
    @GetMapping("/testList")
    @ResponseBody
    public void testList(){
        redisTemplate.opsForList().leftPush("key","111");
        redisTemplate.opsForList().leftPush("key","222");
        redisTemplate.opsForList().leftPush("key","333");

        redisTemplate.opsForList().rightPop("key",3);

        String value = redisTemplate.opsForValue().get("key");
        System.out.println(value);

        redisTemplate.execute((RedisConnection connection)->{
            connection.flushAll();
            return null;
        });
    }
    @GetMapping("/testSet")
    @ResponseBody
    public void testSet() {
        redisTemplate.opsForSet().add("key1","1","2","3");
        Set<String> result = redisTemplate.opsForSet().members("key");
        System.out.println(result);

        System.out.println(redisTemplate.opsForSet().isMember("key","111"));

        redisTemplate.opsForSet().remove("key1","1");
    }
    @GetMapping("/testHash")
    @ResponseBody
    public void testHash() {
        HashMap<String,String> map = new HashMap<>();
        map.put("1","123");
        map.put("2","123");
        map.put("3","123");
        map.put("4","123");

        redisTemplate.opsForHash().putAll("key",map);
        System.out.println(redisTemplate.opsForHash().get("key","1"));


    }
    @GetMapping("/testZSet")
    @ResponseBody
    public void testZSet(){
        redisTemplate.opsForZSet().add("key","zahnsg",12);
        redisTemplate.opsForZSet().add("key","z",11);
        redisTemplate.opsForZSet().add("key","sg",15);
        redisTemplate.opsForZSet().add("key","g",16);

        Set<ZSetOperations.TypedTuple<String>> membersWithScore = redisTemplate.opsForZSet().rangeWithScores("key",0,2);
        System.out.println(membersWithScore);

        redisTemplate.execute((RedisConnection connection)->{
            connection.flushAll();
            return null;
        });
    }
}
























